<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Admin\UserRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class UserController extends Controller
{
    protected $userRepository;
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    /**
     * 获取系统添加用户
     */
    public function getAdminUsers(Request $request)
    {
        $type = (int)$request->get('type');
        $users = $this->userRepository->getByType($type);
        return json_encode($users);
    }
    /**
     * 封号与解封
     */
    public function banUser(Request $request){
        $id = $request->get('user_id');
        $status = $request->get('user_status');
        $result = $this->userRepository->updateStatus($id,$status);
        if($result)
            return json_encode(['code'=>200,'picture_id'=>$id]);
        else
            return json_encode(['code'=>400,'picture_id'=>$id]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->has('user_id')){
            $user_id = $request->get('user_id');
            $user = $this->userRepository->getById($user_id);
            return json_encode($user);
        }else{
            $users = $this->userRepository->getAll();
            return json_encode($users);
        }

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $userData = $request->get('user_data');
        $user = [];
        if($userData) {
            if ($userData['user_thumbnail'])
                $user['user_thumbnail'] = $userData['user_thumbnail'];
            if ($userData['user_name'])
                $user['user_name'] = $userData['user_name'];
            if ($userData['user_nickname'])
                $user['user_nickname'] = $userData['user_nickname'];
            if ($userData['user_sex'])
                $user['user_sex'] = $userData['user_sex'];
            if ($userData['user_password'])
                $user['user_password'] = $userData['user_password'];
            if ($userData['user_wechat_id'])
                $user['user_wechat_id'] = $userData['user_wechat_id'];
            if ($userData['user_mobile_phone'])
                $user['user_mobile_phone'] = $userData['user_mobile_phone'];
            if ($userData['user_province'])
                $user['user_province'] = $userData['user_province'];
            if ($userData['user_city'])
                $user['user_city'] = $userData['user_city'];
            if ($userData['user_money'])
                $user['user_money'] = $userData['user_money'];
            if ($userData['user_rank'])
                $user['user_rank_number'] = (int)$userData['user_rank'];
            if ($userData['user_allow_chat'])
                $user['user_allow_chat'] = $userData['user_allow_chat'];
            if ($userData['user_type'])
                $user['user_type'] = $userData['user_type'];
//            var_dump($user);
//            return json_encode($user);
            $result = $this->userRepository->createUser($user);
            if($result){
                //男/女数增加
                if($userData['user_sex'] == 1){
                    if(Redis::exists('maleNumber')){
                        $maleNumber = Redis::get('maleNumber');
                        $maleNumber++;
                        Redis::set('maleNumber',$maleNumber);
                    }else{
                        Redis::set('maleNumber',1);
                    }
                }else if($userData['user_sex'] == 2) {
                    if(Redis::exists('femaleNumber')){
                        $femaleNumber = Redis::get('femaleNumber');
                        $femaleNumber++;
                        Redis::set('femaleNumber',$femaleNumber);
                    }else{
                        Redis::set('femaleNumber',1);
                    }
                }
                //用户总数增加
                if(Redis::exists('userNumber')){
                    $userNumber = Redis::get('userNumber');
                    $userNumber++;
                    Redis::set('userNumber',$userNumber);
                }else{
                    Redis::set('userNumber',1);
                }
                //今日新增用户增加
                if(Redis::exists('dayUserNumber')){
                    $dayUserNumber = Redis::get('dayUserNumber');
                    $dayUserNumber++;
                    Redis::set('dayUserNumber',$dayUserNumber);
                }else{
                    Redis::set('dayUserNumber',1);
                }

                return json_encode(['code'=>200,'msg'=>'保存成功']);
            }else{
                return json_encode(['code'=>400,'msg'=>'保存失败']);
            }
        }
        return json_encode(['code'=>400,'msg'=>'没有传入值']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $userData = $request->get('user_data');
        $user = [];
        if($userData) {
            if (array_key_exists("user_thumbnail",$userData) && $userData['user_thumbnail'] != null)
                $user['user_thumbnail'] = $userData['user_thumbnail'];
            if (array_key_exists("user_name",$userData) && $userData['user_name'] != null)
                $user['user_name'] = $userData['user_name'];
            if (array_key_exists("user_nickname",$userData) && $userData['user_nickname'] != null)
                $user['user_nickname'] = $userData['user_nickname'];
            if (array_key_exists("user_sex",$userData) && $userData['user_sex'] != null)
                $user['user_sex'] = $userData['user_sex'];
            if (array_key_exists("user_password",$userData) && $userData['user_password'] != null)
                $user['user_password'] = $userData['user_password'];
            if (array_key_exists("user_wechat_id",$userData) && $userData['user_wechat_id'] != null)
                $user['user_wechat_id'] = $userData['user_wechat_id'];
            if (array_key_exists("user_mobile_phone",$userData) && $userData['user_mobile_phone'] != null)
                $user['user_mobile_phone'] = $userData['user_mobile_phone'];
            if (array_key_exists("user_province",$userData) && $userData['user_province'] != null)
                $user['user_province'] = $userData['user_province'];
            if (array_key_exists("user_city",$userData) && $userData['user_city'] != null)
                $user['user_city'] = $userData['user_city'];
            if (array_key_exists("user_money",$userData) && $userData['user_money'] != null)
                $user['user_money'] = $userData['user_money'];
            if (array_key_exists("user_rank_number",$userData) && $userData['user_rank_number'] != null)
                $user['user_rank_number'] = (int)$userData['user_rank_number'];
            if (array_key_exists("user_allow_chat",$userData) && $userData['user_allow_chat'] != null)
                $user['user_allow_chat'] = $userData['user_allow_chat'];
            if (array_key_exists("user_type",$userData) && $userData['user_type'] != null)
                $user['user_type'] = $userData['user_type'];
//            var_dump($user);
//            return json_encode($user);
            $result = $this->userRepository->updateUser($id,$user);
            return json_encode($user);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
